/* eslint-disable */
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const base = require('./webpack.base');
const merge = require('webpack-merge');

class ChunksFromEntryPlugin {
  apply(compiler) {
    compiler.hooks.emit.tap('ChunksFromEntryPlugin', (compilation) => {
      compilation.hooks.htmlWebpackPluginAlterChunks.tap(
        'ChunksFromEntryPlugin',
        (_, {
          plugin
        }) => {
          // takes entry name passed via HTMLWebpackPlugin's options
          const entry = plugin.options.entry;
          const entrypoint = compilation.entrypoints.get(entry);

          return entrypoint.chunks.map((chunk) => ({
            names: chunk.name ? [chunk.name] : [],
            files: chunk.files.slice(),
            size: chunk.modulesSize(),
            hash: chunk.hash
          }));
        }
      );
    });
  }
}

module.exports = merge({
  mode: 'production',
  devtool: 'source-map',
  module: {
    rules: [{
      test: /\.css$/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: ['css-loader']
      })
    },
    {
      test: /\.less$/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: ['css-loader', 'less-loader']
      })
    }
    ]
  },
  plugins: [
    new UglifyJsPlugin({
      sourceMap: false,
      parallel: true
    }),
    new ExtractTextPlugin({
      filename: 'static/css/[name].[hash:7].css'
    }),
    // new ChunksFromEntryPlugin()
  ],
  // optimization: {
  //   splitChunks: {
  //     chunks: 'all',
  //     minSize: 30000,
  //     minChunks: 1,
  //     maxAsyncRequests: 5,
  //     maxInitialRequests: 3,
  //     automaticNameDelimiter: '~',
  //     name: true,
  //     cacheGroups: {
  //       vendors: {
  //         test: /[\\/]node_modules[\\/]/,
  //         priority: -10
  //       },
  //       // default: {
  //       //     minChunks: 2,
  //       //     priority: -20,
  //       //     reuseExistingChunk: true
  //       // },
  //       commons: {
  //         name: 'commons',
  //         chunks: 'initial',
  //         minChunks: 2
  //       }
  //     }
  //   }
  // }
},
  base
);
